/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_base.h"
#include "los_task.h"
#include "los_hwi.h"
#include "los_typedef.h"
#include "pcrambox.h"
#include "los_api_tlsf.h"
#include "los_inspect_entry.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwTskHiID_1;
static UINT32 g_uwTskHiID_2;
static UINT32 g_uwTskHiID_3;

void tlsf_pcram_init()
{
    UINT32 uwRet = LOS_OK;
    UINT32 uwBoxSize = 0x80000UL;

    exmc_pcram_init();
    LOS_pcramboxClr(OS_PCRAM_START_ADDR, OS_PCRAM_START_ADDR, uwBoxSize);
    uwRet = LOS_pcramboxInit((VOID *)OS_PCRAM_START_ADDR, uwBoxSize);
    if (uwRet != LOS_OK)
    {
        printf("TLSF init error\r\n");
    }
    else
    {
        printf("1. TLSF init success!!\r\n");
    }
}

UINT16 *pRet = NULL;
VOID apply_a_memery_1()
{
	printf("\r\n2. apply_a_memery_1()\r\n");
    UINT8 *flag, i;
    char name[] = "allen";

    i = 1;
    flag = &i;
    *flag = 1;
    pRet = SC_MALLOC(name, 0x30000UL, flag);
	printf("\r\n3. %s's pRet is %p\r\n", name, pRet);
    if(pRet == NULL)
    {
        printf("SC_MALLOC FAIL!\r\n");
        LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
    }
    else
    {
        printf("THE Identifier IS %s\r\n", name);
        if(*flag == 1)
        {
            printf("THe fft_table_sclist has been created, the start addr is %p\r\n", (UINT32)pRet);
        }
        else
        {
        }
    }
    LOS_TaskDelete(g_uwTskHiID_1);
}

UINT32 mem_apply_1(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)apply_a_memery_1;
    stInitParam.usTaskPrio = 4;
    stInitParam.pcName = "Rudy_m1";
    stInitParam.uwStackSize = 0x1000;

    uwRet = LOS_TaskCreate(&g_uwTskHiID_1, &stInitParam);
    if (uwRet != LOS_OK)
    {
        printf("mem_apply_1 create Failed!\r\n");
        uwRet = LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
        return LOS_NOK;
    }
    printf("4. mem_apply_1 create Success!\r\n");
    return LOS_OK;
}

VOID apply_a_memery_2()
{
	printf("\r\n5. apply_a_memery_2()\r\n");
    UINT8 *flag, i;
    char name[] = "Bill";

    i = 1;
    flag = &i;
    *flag = 1;
    pRet = SC_MALLOC(name, 0x30000UL, flag);
	printf("\r\n6. %s's pRet is %p\r\n", name, pRet);
    if(pRet == NULL)
    {
        printf("SC_MALLOC FAIL!\r\n");
        LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
    }
    else
    {
        printf("THE Identifier IS %s\r\n", name);
        if(*flag == 1)
        {
            printf("THe fft_table_sclist has been created, the start addr is %p\r\n", (UINT32)pRet);
        }
        else
        {
        }
    }
    LOS_TaskDelay(1000);
    printf("\r\n10. SC_FREE(%s) start. \r\n", name);
    UINT32 freeRes = 1;
    freeRes = SC_FREE(name);
//    if(pRet==0)
    ///SCYHF
    if(freeRes==0)
    {
        printf("11. %s free success!\r\n", name);
    }
    LOS_TaskDelete(g_uwTskHiID_2);
}

UINT32 mem_apply_2(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)apply_a_memery_2;
    stInitParam.usTaskPrio = 4;
    stInitParam.pcName = "Rudy_m2";
    stInitParam.uwStackSize = 0x1000;

    uwRet = LOS_TaskCreate(&g_uwTskHiID_2, &stInitParam);
    if (uwRet != LOS_OK)
    {
        printf("mem_apply_2 create Failed!\r\n");
        uwRet = LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
        return LOS_NOK;
    }
    printf("7. mem_apply_2 create Success!\r\n");
    return LOS_OK;
}

UINT16 *pRet3 = NULL;// start addr of fft
VOID apply_a_memery_3()
{
	printf("\r\n8. apply_a_memery_3()\r\n");
    UINT8 *flag, i;
    char name[] = "Cal";

    i = 1;
    flag = &i;
    *flag = 1;
    pRet3 = NULL;
    UINT8 sc_cnt = 0;
    while(pRet3 == NULL)
    {
        pRet3 = SC_MALLOC(name, 0x30000UL, flag);
        if(sc_cnt==0)
        {
            printf("\r\n9. %s's pRet is %p\r\n", name, pRet3);
        }
    	else
        {
            printf("\r\n12. %s's pRet is %p\r\n", name, pRet3);
        }
        if(pRet3 == NULL)
        {
            printf("%s SC_MALLOC FAIL!\r\n", name);
            sc_cnt++;
            if(sc_cnt>1)
            {
                LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
            }
        }
        else
        {
        	break;
        }
        LOS_TaskDelay(2000);
    }
    printf("THE Identifier IS %s\r\n", name);
    printf("Test TLSF success!\r\n");
    LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_SUCCESS);
    LOS_TaskDelete(g_uwTskHiID_1);
}

UINT32 mem_apply_3(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)apply_a_memery_3;
    stInitParam.usTaskPrio = 4;
    stInitParam.pcName = "Rudy_m3";
    stInitParam.uwStackSize = 0x1000;

    uwRet = LOS_TaskCreate(&g_uwTskHiID_3, &stInitParam);
    if (uwRet != LOS_OK)
    {
        printf("mem_apply_3 create Failed!\r\n");
        uwRet = LOS_InspectStatusSetByID(LOS_INSPECT_TLSF, LOS_INSPECT_STU_ERROR);
        return LOS_NOK;
    }
    printf("mem_apply_3 create Success!\r\n");
    return LOS_OK;
}

UINT32 Example_TLSF(VOID)
{
   // 
    printf("\r\n--------4.4 TLSF test start!--------\r\n");

    // UINT32 uwBoxSize = 0x80000UL;
    // UINT32 uwBlkSize = 512;

    // UINT32 uwRet = LOS_OK;
    

    // exmc_pcram_init();
    // LOS_pcramboxClr(OS_PCRAM_START_ADDR,OS_PCRAM_START_ADDR,uwBoxSize);
    // uwRet = LOS_pcramboxInit((VOID *)OS_PCRAM_START_ADDR, uwBoxSize);
    // if (uwRet != LOS_OK)
    // {
    //     printf("TLSF init error\r\n");
    // }
    // else
    // {
    //     printf("1. TLSF init success!!\r\n");
    // }
    tlsf_pcram_init();
    mem_apply_1();
    mem_apply_2();
    mem_apply_3();

    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
